package com.jstyle.beatecg.app;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;
import android.util.Log;


import com.amap.api.location.AMapLocation;
import com.jstyle.beatecg.ble.BackgroundRunningHelp;
import com.jstyle.beatecg.ble.BleManager;
import com.jstyle.beatecg.dao.DbManager;
import com.jstyle.beatecg.dao.MySqlLiteOpenHelper;
import com.jstyle.beatecg.util.SharePreferenceUtils;


/**
 * Created by Administrator on 2017/4/11.
 */

public class MyApplication extends MultiDexApplication {
    private static final String TAG = "MyApplication";
    private MySqlLiteOpenHelper mySqlLiteOpenHelper;
    public  static Context context;
    public  static AMapLocation aMapLocation;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    @Override
    public void onCreate() {
        super.onCreate();
        context = this.getApplicationContext();
        String mProcessName = getCurrentProcessName(this);
        Log.i(TAG, "onCreate" + "getProcessName：" + mProcessName);
        Log.i(TAG, "init_all_process");
        if(TextUtils.equals("com.jstyle.sleep:AlarmReceiver", mProcessName)){
            return;
        }

//        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
//                .setDefaultFontPath("W3.OTF")
//                build()
//        );
       DbManager.init(this);
     //   ButterK.nife.setDebug(true);
        BleManager.init(this);
        Log.i(TAG, "onCreate: ");
        //Fresco.initialize(this);
        SharePreferenceUtils.init(this);
        startService(new Intent(this,BackgroundRunningHelp.class));//开启后台音乐
        //MobSDK.init(this,"1de714fd271e4","ce58ec0f6518d81c2e168bae6c92a12b");
     /*   String address= SharePreferenceUtils.getSpString(SharePreferenceUtils.KEY_ADDRESS);
        if(!TextUtils.isEmpty(address))
            BleManager.getInstance().connectDevice(address);*/

    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        Log.i(TAG, "onTerminate: ");
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        Log.i(TAG, "onTrimMemory: ");
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Log.i(TAG, "onLowMemory: ");
    }

    public BleManager getBleManager(){
        return BleManager.getInstance();
    }

    private String getCurrentProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }
}
